//
//  YMComposePictureView.swift
//  YMWeiBo
//
//  Created by mimi on 16/7/26.
//  Copyright © 2016年 mimi. All rights reserved.
//

import UIKit

let margin = 5
let itemW = (SCREENW - 20 - 20)/3


class YMComposePictureView: UICollectionView {
    //定义闭包通知vc弹出相册
    var closure:(()->())?
    //保存图片数组
    var imageList:[UIImage] = [UIImage]()
    

    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func setupUI() {
        dataSource = self
        delegate = self
        
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        layout.itemSize = CGSize(width: itemW, height: itemW)
        
        registerClass(YMComposePictureViewCell.self, forCellWithReuseIdentifier: "cell")
        backgroundColor = RandomColor()
    }
    
    func addImages(image:UIImage) {
        
        imageList.append(image)
        
        hidden = false
        reloadData()
    }

}


extension YMComposePictureView:UICollectionViewDataSource,UICollectionViewDelegate{
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        if imageList.count == 0 || imageList.count == 9{
            return imageList.count
        }else{
            
            return imageList.count + 1
        }
        
    }
    
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! YMComposePictureViewCell
//        cell.backgroundColor = RandomColor()
        
        if indexPath.item == imageList.count{
            cell.image = nil
        }else{
            cell.image = imageList[indexPath.item]
        }
        
        cell.closure = {[weak self] in
            
            self?.imageList.removeAtIndex(indexPath.item)
            
            if self?.imageList.count == 0{
                self?.hidden = true
            }
            self?.reloadData()
        }
        
        return cell
    }
    
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        if indexPath.item == imageList.count{
            closure?()
        }
    }
    
}

//自定义cell

class YMComposePictureViewCell: UICollectionViewCell{
    
    lazy var bgImageView: UIImageView = UIImageView()
    
    lazy var deleteButton: UIButton = {
       let button = UIButton()
        button.addTarget(self, action: #selector(YMComposePictureViewCell.buttonClick), forControlEvents: .TouchUpInside)
        button.setImage(UIImage(named: "compose_photo_close"), forState: .Normal)
        return button
    }()
    
    var closure:(()->())?
    
    var image:UIImage?{
        
        didSet{
            
            if image == nil {
                bgImageView.image = UIImage(named: "compose_pic_add")
                bgImageView.highlightedImage = UIImage(named: "compose_pic_add_highlighted")
                
                deleteButton.hidden = true
            }else{
                bgImageView.image = image
                bgImageView.highlightedImage = nil
                deleteButton.hidden = false
                
            }
        }
  
    }
    
    func buttonClick() {
        
        closure?()
    }
    
    
    func setupUI() {
       
        contentView.addSubview(bgImageView)
        contentView.addSubview(deleteButton)
        
        bgImageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
        deleteButton.snp_makeConstraints { (make) -> Void in
            make.top.trailing.equalTo(contentView)
        }
        
    }
    
   override init(frame: CGRect) {
        super.init(frame: frame)
       setupUI()
    }
    
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}















